/**
 * 多选组件
 */
(function(window, angular, undefined){

	var component = angular.module('water.common.component.comSingleCheck',[]);
	
	component.directive("comSingleCheck", function($timeout) {
		  return { 
			require : "ngModel",
		    restrict: "E",        // 指令是一个元素 (并非属性) 
		    scope: {              // 设置指令对于的scope 
		      id: "@",          // id 值传递 （字符串，单向绑定）
		      model:"=ngModel",
		      data: "=", //双向绑定
		      change:"&ngChange",
		      singleKey:"@singleKey",//互斥key，选中了这个key其他的就自动清空
		      more:"@ngMore" //大于几个就显示更多按钮
		    }, 
		    template:             // 替换HTML (使用scope中的变量)
		      '<div >'+
		      '<ul class="zs-data-sizer" id={{id}} style="float:left;">'+
		      ' <li ng-show="!needMore || show || ($index <= more)" repeat-finish ng-finish="repeatFinish()" ng-repeat="item in data" ng-click="uiClick(item)" ng-class="isContain(item)?\'selected\':\'\'"><a href="#" >{{item.v}}</a></li>'+
		      '</ul><a ng-click="moreClick()" ng-show="data.length>more" href="#" style="color: #908D8D;text-decoration: underline;float:left;padding: 3px 0px;">{{moreText}}</a></div>', 
		    replace: true,        // 使用模板替换原始标记 
		    transclude: false,    // 不复制原始HTML内容 
		    link: function ($scope, element, attrs, controller) {
		    	
		    	$scope.selectModels;
		    	$scope.show = false;
		    	$scope.moreText = "展开>>";
		    	$scope.singleKey = attrs.singleKey;
		    	$scope.controller = controller;
		    	$scope.modelFlag = attrs.tag;
		    	
		    	$scope.moreClick = function(){
		    		$scope.show=!$scope.show;
		    		if($scope.show){
		    			$scope.moreText = "收起<<";
		    		}
		    		else{
		    			$scope.moreText = "展开>>";
		    		}
		    	}
		    	
		    	try{
		    		/**
			    	 * 接口
			    	 */
			    	$scope.componentObject = {
			    			getSelectModel:function(){
			    				return $scope.selectModels;
			    			}
			    	};
		    	} catch (e) {
					console.log(e)
				}
		    	
		    	if($scope.more){
		    		$scope.needMore = true;//打开更多的控制
		    	}
		    	
		    	/**
		    	 * 判断子项是否选中
		    	 */
		    	$scope.isContain = function(item){
		    		
		    		var result = false;
		    		
		    		if($scope.model == undefined){
		    			return false;
		    		}
		    		
		    		result = $scope.model == item.k;
		    		
	    			return result;
		    	}
		    	
		    	/**
		    	 * 获取model的值
		    	 */
		    	$scope.getModelValue = function(model){
		    		return model;
		    	}
		    	
		    	/**
		    	 * 选择事件
		    	 */
		    	$scope.uiClick = function(item){
		    		if(!$scope.isContain(item)){
		    			$scope.model = item.k;
		    		}
		    	}
		    	
		    	controller.$render = function(){
		    		if(!controller.$viewValue || !$scope.data){
		    			return;
		    		}
		    		
		    		$scope.model = controller.$viewValue;
		    		
		    		for(var i=0;i<$scope.data.length;i++){
		    			var dataItem = $scope.data[i];
		    			if(dataItem.k == $scope.model){
		    				$scope.selectModels = dataItem;
		    				break;
		    			}
		    		}
		    		
		    		if($scope.change()){
		    			$scope.change()($scope.model,$scope.selectModels);
		    		}
		    	}
		    	
		    }
		  } 
		});
	
}(window, angular));